package com.classic.mafiacards;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

/**
 * Created by pavilion on 11/15/13.
 */
public class CardsActivity extends Activity implements View.OnClickListener{
    static final int CITIZEN = 0;
    static final int MAFIA = 1;
    static final int SHERIFF = 2;
    static final int DON = 3;

    static final int[] citizens_list = {R.drawable.citizen1, R.drawable.citizen2, R.drawable.citizen3, R.drawable.citizen4, R.drawable.citizen5};
    static final int[] mafia_list  = {R.drawable.mafia1, R.drawable.mafia2};
    static final int[] sheriff_list = {R.drawable.sheriff1, R.drawable.sheriff2};

    static ArrayList<Integer> cards = new ArrayList<Integer>();
    static ArrayList<Player> players = new ArrayList<Player>();

    TextView text;
    ImageView clicker;

    public int getRole() {

        if (players.size() == 0) {
            int mafia_number = Integer.parseInt(PreferenceManager
                    .getDefaultSharedPreferences(this).getString("mafia", "3"));
            int players_number = Integer.parseInt(PreferenceManager
                    .getDefaultSharedPreferences(this).getString("players", "10"));
            cards.clear();
            cards.add(SHERIFF);
            cards.add(DON);
            cards.addAll(Collections.nCopies(mafia_number - 1, MAFIA));
            cards.addAll(Collections.nCopies(players_number - cards.size(), CITIZEN));
        }

        int card_possition;
        int role;
        card_possition = new Random().nextInt(cards.size());
        role = cards.get(card_possition);
        cards.remove(card_possition);
        return role;
    }

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        OptionsReader.updateLanguage(this);
        setContentView(R.layout.activity_cards);

        clicker = (ImageView) findViewById(R.id.invisiableButton);
        text = (TextView) findViewById(R.id.textView);
        clicker.setOnClickListener(this);
        text.setOnClickListener(this);

        Typeface tf = Typeface.createFromAsset(this.getAssets(), "western.ttf");
        text.setTypeface(tf);

        Player player = new Player(String
                .format(getString(R.string.player_title), players.size() + 1),
                players.size(), getRole());
        players.add(player);

        if (PreferenceManager.getDefaultSharedPreferences(this)
                .getBoolean("position", false))
            text.setText((player.possition + 1) + " ");
        text.append(getResources().getStringArray(R.array.roles)[player.role].toUpperCase());

        switch(player.role) {
            case CITIZEN: {
                clicker.setBackgroundResource(citizens_list[new Random().nextInt(
                        (PreferenceManager.getDefaultSharedPreferences(this).
                                getBoolean("cards_skin", true) ? 1 : 0) * 4 + 1)]);
                text.setTextColor(getResources().getColor(android.R.color.background_dark));
                text.setBackgroundColor(getResources().getColor(android.R.color.background_light));
                break;
            }
            case MAFIA: {
                clicker.setBackgroundResource(mafia_list[new Random().nextInt(2)]);
                text.setTextColor(getResources().getColor(android.R.color.background_light));
                text.setBackgroundColor(getResources().getColor(android.R.color.background_dark));
                break;
            }
            case DON: {
                clicker.setBackgroundResource(R.drawable.don);
                text.setTextColor(getResources().getColor(android.R.color.background_light));
                text.setBackgroundColor(getResources().getColor(android.R.color.background_dark));
                break;
            }
            case SHERIFF: {
                clicker.setBackgroundResource(sheriff_list[new Random().nextInt(2)]);
                text.setTextColor(getResources().getColor(android.R.color.background_dark));
                text.setBackgroundColor(getResources().getColor(android.R.color.background_light));
                break;
            }
        }

    }

    @Override
    public void onClick(View v) {
        if(cards.size() != 0) {
            startActivity(new Intent(CardsActivity.this, CardsActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        } else {
            startActivity(new Intent(CardsActivity.this, TimerActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
        }
        v.setEnabled(false);
        finish();
    }

    public void onBackPressed() {
        startActivity(new Intent(CardsActivity.this, MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
        finish();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_shuffle_cards, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
                this.onBackPressed();
                return true;

    }
}